using System;
using System.Data;

namespace Widgetsphere.Core.DataAccess
{
	public abstract partial class PersistableDomainObjectBase : DataRow, IPersistableDomainObject
	{
		protected bool _eventsEnabled = true;

		public PersistableDomainObjectBase(DataRowBuilder drb)
			: base(drb)
		{
		}

		#region IPersistableDomainObject Members

		public event EventHandler BeforeUpdate;
		public event EventHandler AfterUpdate;
		public event EventHandler BeforeInsert;
		public event EventHandler AfterInsert;
		public event EventHandler BeforeDelete;
		public event EventHandler AfterDelete;
		public abstract void Persist();
		public abstract void Remove();

		#endregion

		#region public on methods

		public virtual void OnBeforeUpdate(object sender, System.EventArgs e)
		{
			if(BeforeUpdate != null && EventsEnabled)
			{
				BeforeUpdate(sender, e);
			}
		}

		public virtual void OnAfterUpdate(object sender, System.EventArgs e)
		{
			if(AfterUpdate != null && EventsEnabled)
			{
				AfterUpdate(sender, e);
			}
		}

		public virtual void OnBeforeInsert(object sender, System.EventArgs e)
		{
			if(BeforeInsert != null && EventsEnabled)
			{
				BeforeInsert(sender, e);
			}
		}

		public virtual void OnAfterInsert(object sender, System.EventArgs e)
		{
			if(AfterInsert != null && EventsEnabled)
			{
				AfterInsert(sender, e);
			}
		}

		public virtual void OnBeforeDelete(object sender, System.EventArgs e)
		{
			if(BeforeDelete != null && EventsEnabled)
			{
				BeforeDelete(sender, e);
			}
		}

		public virtual void OnAfterDelete(object sender, System.EventArgs e)
		{
			if(AfterDelete != null && EventsEnabled)
			{
				AfterDelete(sender, e);
			}
		}

		#endregion

		public bool EventsEnabled
		{
			get { return _eventsEnabled; }
			set { _eventsEnabled = value; }
		}

	}
}
